#!/bin/bash

# 定义进度条函数
progress() {
  local main_pid=$1
  local app_name=$2
  local start_time=$(date +%s.%N)
  local length=20
  local ratio=1
  while [ "$(ps -p $main_pid | wc -l)" -ne "1" ]; do
    mark='>'
    progress_bar=
    for i in $(seq 1 $length); do
      if [ "$i" -gt "$ratio" ]; then
        mark='-'
      fi
      progress_bar="$progress_bar$mark"
    done
    local current_time=$(date +%s.%N)
    local elapsed_time=$(echo "$current_time - $start_time" | bc -l | awk '{printf "%.1f", $1}')
    printf "$app_name: $progress_bar (%ss)\r" "$elapsed_time"
    ratio=$((ratio+1))
    if [ "$ratio" -gt "$length" ]; then
      ratio=1
    fi
    sleep 0.1
  done
}


# 定义应用程序安装函数
install_app1() {
  sleep 10
}

install_app2() {
  sleep 10
}

# 安装应用程序1
install_app1 & install_app1_pid=$(jobs -p | tail -1)
progress "$install_app1_pid" "应用1安装中" & progress_pid=$(jobs -p | tail -1)
wait "$install_app1_pid"
printf "应用1安装中: 完成\n"

# 安装应用程序2
install_app2 & install_app2_pid=$(jobs -p | tail -1)
progress "$install_app2_pid" "应用2安装中" & progress_pid=$(jobs -p | tail -1)
wait "$install_app2_pid"
printf "应用2安装中: 完成\n"
